package com.bnyy.medicalHousekeeper.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bnyy.medicalHousekeeper.R;
import com.bnyy.medicalHousekeeper.activity.ArticleDetailActivity;
import com.bnyy.medicalHousekeeper.activity.ArticleListActivity;
import com.bnyy.medicalHousekeeper.activity.GoodsActivity;
import com.bnyy.medicalHousekeeper.activity.IndexUserListActivity;
import com.bnyy.medicalHousekeeper.activity.NormalGoodsDetailActivity;
import com.bnyy.medicalHousekeeper.activity.ServiceGoodsDetailActivity;
import com.bnyy.medicalHousekeeper.adapter.IndexGoodsAdapter;
import com.bnyy.medicalHousekeeper.adapter.IndexArticleAdapter;
import com.bnyy.medicalHousekeeper.adapter.IndexUserAdapter;
import com.bnyy.medicalHousekeeper.base.BaseFragmentImpl;
import com.bnyy.medicalHousekeeper.bean.Shop;
import com.bnyy.medicalHousekeeper.bean.Customer;
import com.bnyy.medicalHousekeeper.bean.Goods;
import com.bnyy.medicalHousekeeper.bean.Article;
import com.bnyy.medicalHousekeeper.bean.IndexData;
import com.bnyy.common.adapter.BaseNormalListAdapter;
import com.bnyy.medicalHousekeeper.bean.Subordinate;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;

import butterknife.BindView;

/**
 * Author : 关炳鹏
 * Description :
 * Date : 2023/2/3
 **/
public class IndexDatasFragment extends BaseFragmentImpl {
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.iv_no_data)
    ImageView ivNoData;
    @BindView(R.id.tv_no_data)
    TextView tvNoData;
    @BindView(R.id.ll_no_data)
    LinearLayout llNoData;
    @BindView(R.id.tv_check_all)
    TextView tvCheckAll;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_index_datas;
    }

    public static IndexDatasFragment getInstance(IndexData.Data.Type type) {
        return getInstance(type, false);
    }

    public static IndexDatasFragment getInstance(IndexData.Data.Type type, boolean viewAll) {
        IndexDatasFragment fragment = new IndexDatasFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("type", type);
        bundle.putBoolean("viewAll", viewAll);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        llNoData.setVisibility(View.VISIBLE);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));

        Bundle bundle = getArguments();
        if (bundle != null) {
            boolean viewAll = bundle.getBoolean("viewAll", false);
            IndexData.Data.Type type = (IndexData.Data.Type) bundle.getSerializable("type");
            if (type != null) {
                ArrayList<Object> items = type.getItems();

                if (items != null && items.size() > 0) {
                    llNoData.setVisibility(View.GONE);
                    Gson gson = new Gson();
                    String json = gson.toJson(viewAll ? items : items.size() > 4 ? items.subList(0, 4) : items);

                    switch (type.getType()) {
                        case 1: // 图文分享
                            IndexArticleAdapter indexGoodsArticleAdapter = new IndexArticleAdapter(mContext, new BaseNormalListAdapter.OnItemClickListener<Article>() {
                                @Override
                                public void onClick(View view, Article article, int position) {
                                    ArticleDetailActivity.showByArticleId(mContext, article);
                                }
                            });
                            recyclerView.setAdapter(indexGoodsArticleAdapter);
                            indexGoodsArticleAdapter.refresh(gson.fromJson(json, new TypeToken<ArrayList<Article>>() {
                            }.getType()));
                            break;
                        case 2: // 商品列表
                        case 3: // 新品
                            IndexGoodsAdapter indexGoodsAdapter = new IndexGoodsAdapter(mContext, new BaseNormalListAdapter.OnItemClickListener<Goods>() {
                                @Override
                                public void onClick(View view, Goods goods, int position) {
                                    switch (goods.getType()) {
                                        case 1:
                                            NormalGoodsDetailActivity.show(mContext, goods);
                                            break;
                                        case 2:
                                        case 3:
                                            ServiceGoodsDetailActivity.show(mContext, goods.getId());
                                            break;
                                    }
                                }
                            });
                            recyclerView.setAdapter(indexGoodsAdapter);
                            indexGoodsAdapter.refresh(gson.fromJson(json, new TypeToken<ArrayList<Goods>>() {
                            }.getType()));
                            break;
                        case 4: // 健康异常
                        case 5: // 下级医养管家
                        case 6: // 商家列表
                        case 7: // 用户列表
                            IndexUserAdapter indexUserAdapter = new IndexUserAdapter(mContext);
                            recyclerView.setAdapter(indexUserAdapter);
                            switch (type.getType()) {
                                case 5: // 下级医养管家
                                    indexUserAdapter.refresh(gson.fromJson(json, new TypeToken<ArrayList<Subordinate>>() {
                                    }.getType()));
                                    break;
                                case 6: // 商家列表
                                    indexUserAdapter.refresh(gson.fromJson(json, new TypeToken<ArrayList<Shop>>() {
                                    }.getType()));
                                    break;
                                case 4: // 下健康异常
                                case 7: // 用户列表
                                    indexUserAdapter.refresh(gson.fromJson(json, new TypeToken<ArrayList<Customer>>() {
                                    }.getType()));
                                    break;
                            }
                            break;
                    }
                }

                tvCheckAll.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        switch (type.getType()) {
                            case 1: // 图文分享
                                ArticleListActivity.show(mContext);
                                break;
                            case 2: // 商品列表
                            case 3: // 新品
                                GoodsActivity.show(mContext, type);
                                break;
                            case 4: // 健康异常用户
                            case 5: // 下级医养管家
                            case 6: // 商家列表
                            case 7: // 用户列表
                                IndexUserListActivity.show(mContext, type);
                                break;
                        }
                    }
                });
            }
        }
    }
}

































